package org.hxzon.demo.scala.matchdemo

object Symbol {
  def unapply(symbol: String): Boolean = symbol == "GOOG" || symbol == "IBM"
}
object ReceiveStockPrice {
  def unapply(input: String): Option[(String, Double)] = {
    try {
      if (input contains ":") {
        val splitQuote = input split ":"
        Some(splitQuote(0), splitQuote(1).toDouble)
      } else {
        None
      }
    } catch {
      case _: NumberFormatException => None
    }
  }
}
object UnapplyMatch {
  def process(input: String) {
    input match {
      case Symbol() => println("Look up price for valid symbol " + input)
      //case ReceiveStockPrice(symbol, price) => printf("Received price %f for symbol %s\n", price, symbol)
      case ReceiveStockPrice(symbol @ Symbol(), price) => printf("Received price %f for symbol %s\n", price, symbol)
      //first,Option(symbol,price)=ReceiveSotckPrice.unapply(input)
      //then,Boolean=Symbol.unapply(symbol)
      case _ => println("Invalid input " + input)
    }
  }
  def main(args: Array[String]): Unit = {
    process("GOOG") //Look up price for valid symbol GOOG
    process("GOOG:310.84") //Received price 310.840000 for symbol GOOG
    process("GOOG:BUY") //Invalid input GOOG:BUY
    process("ERR:12.21") //Received price 12.210000 for symbol ERR when case ReceiveStockPrice(symbol, price)
    //Invalid input ERR:12.21 when case ReceiveStockPrice(symbol @ Symbol(),price)
  }
}
